﻿@model dynamic
@{
    ViewData["Title"] = Model.title;
}

<div class="mini-toolbar" style="padding:2px;border-bottom:0;">
    <table style="width:100%;">
        <tr>
            <td style="width:100%;">
                <a id="a_add" plain="true" class="mini-button" iconcls="icon-save" title="保存" onclick="saveClick">保存</a>
                <a id="a_del" plain="true" class="mini-button" iconcls="icon-cancel" title="关闭" onclick="cancelClick">关闭</a>
            </td>
        </tr>
    </table>
</div>
<div style="padding:2px;border-bottom:0;">
    <form id="frmEdit" method="post" action="">
        <input type="hidden" class="mini-hidden" name="role_id" value="@Model.role_id" id="role_id" />
        
        <ul id="trMenu" class="mini-tree" url="/sys/role/GetRoleMenu4Edit?id=@Model.role_id" style="width:200px;padding:5px;"
            showTreeIcon="true" textField="name" idField="menu_id" parentField="parent_id" resultAsTree="false"
            showCheckBox="true"   allowSelect="false" enableHotTrack="false"
            checkRecursive="false" autoCheckParent="false" onnodecheck="onMenuChecked"
            ></ul> 
    </form>
</div>
<script type="text/javascript">
        mini.parse();
        function closeClick() {
            sx.winClose({ sucess: true });
        }
        function cancelClick() {
            sx.winCancel();
        }
        function saveClick() {
            var form = new mini.Form("#frmEdit");
            var data = form.getData(true);

            var tree = mini.get("trMenu");
            var value = tree.getValue(true); 
            $.ajax({
                url: "/sys/Role/SaveRoleMenuAjax",
                type: "post",
                data: {
                    role_id: mini.get("role_id").getValue(),
                    menu_ids: value,
                },
                dataType:"json",
                success: function (data) {
                    if (data.resultCode == sx.resultCode.ok) {
                        sx.success("保存成功！", function () { closeClick(); });
                    } else {
                        sx.error(data.info);
                    }
                }
            });
        }
        // 如果子节点选中， 则选中父节点
        function onMenuChecked(e) {  
            var menu = e.sender; 
            var node = e.node; 
            if (node.checked) {
                checkParentNode(menu, node.parent_id); 
            }
        }
        function checkParentNode(menu, pid) { 
            var pnode = menu.getNodesByValue(pid); 
            if (pnode.length > 0) { 
                menu.checkNode(pnode[0]);
                checkParentNode(menu, pnode[0].parent_id );
            } 
        }
</script> 